package com.cxhc.system.service.hcdoc.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cxhc.common.utils.DateUtils;
import com.cxhc.system.domain.hcdoc.HdParkMonthly;
import com.cxhc.system.domain.hcdoc.HdProjectDocInfo;
import com.cxhc.system.domain.hcdoc.HdProjectInfo;
import com.cxhc.system.mapper.hcdoc.HdParkMonthlyMapper;
import com.cxhc.system.mapper.hcdoc.HdProjectDocInfoMapper;
import com.cxhc.system.service.hcdoc.IHdParkMonthlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 停车包月信息Service业务层处理
 * 
 * @author Dhq
 * @date 2024-09-26
 */
@Service
public class HdParkMonthlyServiceImpl implements IHdParkMonthlyService
{
    @Autowired
    private HdParkMonthlyMapper hdParkMonthlyMapper;

    @Autowired
    private HdProjectDocInfoMapper hdProjectDocInfoMapper;

    /**
     * 查询停车包月信息
     * 
     * @param id 停车包月信息主键
     * @return 停车包月信息
     */
    @Override
    public HdParkMonthly selectHdParkMonthlyById(Long id)
    {
        HdParkMonthly hdParkMonthly = hdParkMonthlyMapper.selectHdParkMonthlyById(id);
        List<HdProjectDocInfo> hdProjectDocInfos = hdProjectDocInfoMapper.selectListByProjectIdAndModule(String.valueOf(hdParkMonthly.getId()) ,"4");
        hdParkMonthly.setHdProjectDocInfos(hdProjectDocInfos);
        return hdParkMonthly;
    }

    /**
     * 查询停车包月信息列表
     * 
     * @param hdParkMonthly 停车包月信息
     * @return 停车包月信息
     */
    @Override
    public List<HdParkMonthly> selectHdParkMonthlyList(HdParkMonthly hdParkMonthly)
    {
        return hdParkMonthlyMapper.selectHdParkMonthlyList(hdParkMonthly);
    }

    @Override
    public Map<String,Object> amount() {
        Map<String,Object> map = new HashMap<>();
        String amount = hdParkMonthlyMapper.amount();
        map.put("amount",amount);
        return map;
    }

    /**
     * 新增停车包月信息
     * 
     * @param hdParkMonthly 停车包月信息
     * @return 结果
     */
    @Override
    public int insertHdParkMonthly(HdParkMonthly hdParkMonthly)
    {
        List<HdParkMonthly> hdParkMonthlies = hdParkMonthlyMapper.selectHdParkMonthlyByPlatNumber(hdParkMonthly.getPlatNumber());
        if (!CollectionUtils.isEmpty(hdParkMonthlies)){
            for (HdParkMonthly parkMonthly : hdParkMonthlies) {
                if (!(DateUtils.dateTime(DateUtils.YYYY_MM_DD,parkMonthly.getEndDate()).before(DateUtils.dateTime(DateUtils.YYYY_MM_DD,hdParkMonthly.getStartDate()))
                || DateUtils.dateTime(DateUtils.YYYY_MM_DD,hdParkMonthly.getEndDate()).before(DateUtils.dateTime(DateUtils.YYYY_MM_DD,parkMonthly.getStartDate()))
                )){
                    throw new RuntimeException("当前时间区间包含已包时间，请调整后再试！！！");
                }
            }
        }
        int insert = hdParkMonthlyMapper.insertHdParkMonthly(hdParkMonthly);
        if (1 == insert){
            List<HdProjectDocInfo> hdProjectDocInfos = hdParkMonthly.getHdProjectDocInfos();
            if (!CollectionUtils.isEmpty(hdProjectDocInfos)){
                for (HdProjectDocInfo hdProjectDocInfo : hdProjectDocInfos) {
                    //文档所属模块1-项目中心 2-信访件 3-年度资料 4-停车包月 5-小额招标
                    hdProjectDocInfo.setModule("4");
                    hdProjectDocInfo.setProjectInfoId(String.valueOf(hdParkMonthly.getId()));
                }
                hdProjectDocInfoMapper.batchInsertDoc(hdProjectDocInfos);
            }
        }
        return insert;
    }

    /**
     * 修改停车包月信息
     * 
     * @param hdParkMonthly 停车包月信息
     * @return 结果
     */
    @Override
    public int updateHdParkMonthly(HdParkMonthly hdParkMonthly)
    {
        return hdParkMonthlyMapper.updateHdParkMonthly(hdParkMonthly);
    }

    /**
     * 批量删除停车包月信息
     * 
     * @param ids 需要删除的停车包月信息主键
     * @return 结果
     */
    @Override
    public int deleteHdParkMonthlyByIds(Long[] ids)
    {
        return hdParkMonthlyMapper.deleteHdParkMonthlyByIds(ids);
    }

    /**
     * 删除停车包月信息信息
     * 
     * @param id 停车包月信息主键
     * @return 结果
     */
    @Override
    public int deleteHdParkMonthlyById(Long id)
    {
        return hdParkMonthlyMapper.deleteHdParkMonthlyById(id);
    }
}
